#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnDebug_clicked()
{
    QString str;
    double num = 12345.12345;
    qDebug("num = %f", num);
    str.setNum(num, 'f', 5);
    qDebug("str = %s", str.toLocal8Bit().data());
    str.setNum(num, 'E', 5);
    qDebug("str = %s", str.toLocal8Bit().data());
    str.setNum(num, 'g', 5);
    qDebug("str = %s", str.toLocal8Bit().data());
    str.setNum(num, 'g', 3);
    qDebug("str = %s", str.toLocal8Bit().data());
}

void Widget::on_btnCal_clicked()
{
    int num = ui->editNum->text().toInt();
    float price = ui->editPrice->text().toDouble();
    float total =  num * price;
    QString str;
    str.setNum(total, 'f', 2);
    ui->editTotal->setText(str);
}

void Widget::on_btnDec_clicked()
{
    int num = ui->editDec->text().toInt();
    QString str;
    //ui->editBin->setText(str.setNum(num, 2));
    ui->editBin->setText(QString::number(num, 2));
    //ui->editHex->setText(str.setNum(num, 16));
    //str = QString::asprintf("0x%x", num);
    str = QString("0x%1").arg(num, 0, 16);
    ui->editHex->setText(str);
}

void Widget::on_btnBin_clicked()
{
    QString str;
    bool ok;
    int num = ui->editBin->text().toInt(&ok, 2);
    if(ok)
    {
        ui->editDec->setText(str.setNum(num, 10));
        str = QString::asprintf("0x%x", num);
        ui->editHex->setText(str);
    }
}

void Widget::on_btnHex_clicked()
{
    QString str;
    bool ok;
    QString numStr =  ui->editHex->text().last(ui->editHex->text().size() - 2);
    int num = numStr.toInt(&ok, 16);
    if(ok)
    {
        ui->editDec->setText(str.setNum(num, 10));
        ui->editBin->setText(str.setNum(num, 2));
    }
}

